package top.shiyiri.shops.controller;

import com.alibaba.fastjson.JSONObject;
import top.shiyiri.shops.bean.EraHotCateTop3;
import top.shiyiri.shops.service.EraHotCateTop3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Aunean
 * @date 2021/12/23
 */
@Controller
public class EraHotCateTop3Controller {

    @Autowired
    private EraHotCateTop3Service service;

    @RequestMapping("/findEraHotCateTop3ByPage")
    @ResponseBody
    public String findEraHotCateTop3ByPage(int page, HttpSession session) {

        List<EraHotCateTop3> list = service.findEraHotCateTop3ByPage(page, 6);
        String jsonArr = JSONObject.toJSONString(list);

        int lines = service.totalLines();

        int pages = lines%6==0? lines/6: lines/6 + 1;

        session.setAttribute("pages", pages);
        session.setAttribute("page", page);
        return jsonArr;
    }

    @RequestMapping("/getEraHotCateTop3Pages")
    @ResponseBody
    public Integer getEraHotCateTop3Pages(HttpSession session) {

        return (int) (Integer) session.getAttribute("pages");
    }

    @RequestMapping("/findEraHotCateTop3")
    @ResponseBody

    public String findEraHotCateTop3() {

        List<EraHotCateTop3> list = service.findEraHotCateTop3();

        return JSONObject.toJSONString(list);
    }

}
